package net.hn.hnms.biz.attendance.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.attendance.domain.OdsIrisAlcoholQuestionRecord;
import net.hn.hnms.biz.attendance.domain.OdsIrisAttendRecord;
import net.hn.hnms.biz.attendance.request.OdsIrisAlcoholQuestionRecordReq;
import net.hn.hnms.biz.attendance.request.OdsIrisAttendRecordReq;

import net.hn.hnms.biz.attendance.service.OdsIrisAlcoholQuestionRecordService;
import net.hn.hnms.biz.attendance.service.OdsIrisAttendRecordService;

import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;
import net.hn.hnms.sys.common.core.domain.R;

import net.hn.hnms.sys.common.core.web.controller.BaseController;

import net.hn.hnms.sys.common.satoken.config.AutoSetMineCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

/**
 * 考勤模块
 *
 * @author gzr
 * @date 2022-08-03
 */

@Api(value = "考勤模块", tags = {"考勤模块"})
@RestController
//@RequestMapping("/attendace")
@RequestMapping("/api")
public class AttendaceController extends BaseController {


    @Autowired
    private OdsIrisAttendRecordService odsIrisAttendRecordService;

    @Autowired
    private OdsIrisAlcoholQuestionRecordService odsIrisAlcoholQuestionRecordService;

    @ApiOperation("分页获取考勤列表")
//    @SaCheckPermission("plan:sale:page")
    @GetMapping ("/page")
    @AutoSetMineCode
    public R<Page<OdsIrisAttendRecord>> page(OdsIrisAttendRecordReq req) {
        long current = 1;
        long size = 10;
        //User userQuery = new User();
        OdsIrisAttendRecord odsIrisAttendRecord = new OdsIrisAttendRecord();
        if (req != null) {
            BeanUtils.copyProperties(req, odsIrisAttendRecord);
             current = req.getCurrent();
            size = req.getPageSize();
        }
        LambdaQueryWrapper<OdsIrisAttendRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .like(StrUtil.isNotBlank(req.getNameOrPersonNumber()), OdsIrisAttendRecord::getPersonName, req.getNameOrPersonNumber()).or()
                .like(StrUtil.isNotBlank(req.getNameOrPersonNumber()), OdsIrisAttendRecord::getPersonNumber, req.getNameOrPersonNumber())
                .eq(StrUtil.isNotBlank(req.getDepartName()),OdsIrisAttendRecord::getDepartName,req.getDepartName())
                .eq(StrUtil.isNotBlank(req.getShiftTypeName()),OdsIrisAttendRecord::getShiftTypeName,req.getShiftTypeName())
                .ge(req.getBeginDateTime() != null, OdsIrisAttendRecord::getAttendDate, req.getBeginDateTime())
                .le(req.getEndDateTime() != null, OdsIrisAttendRecord::getAttendDate, req.getEndDateTime());
//                .orderByDesc(OdsIrisAttendRecord::getRecognizeTime);
        if (req.getIsSignin()){
            queryWrapper.isNull(req.getIsSignin(),OdsIrisAttendRecord::getBeginTime);
        }
        if (req.getIsSignout()){
            queryWrapper.isNull(req.getIsSignout(),OdsIrisAttendRecord::getEndTime);
        }
        Page<OdsIrisAttendRecord> userPage = odsIrisAttendRecordService.page(new Page<>(current, size), queryWrapper);



        return R.ok(userPage);
    }

    @ApiOperation("酒检结果分页查询")
//    @SaCheckPermission("plan:sale:page")
    @GetMapping("/page2")
    @AutoSetMineCode
    public R<Page<OdsIrisAlcoholQuestionRecord>> page2(OdsIrisAlcoholQuestionRecordReq req) {
        long current = 1;
        long size = 10;
        //User userQuery = new User();
        OdsIrisAttendRecord odsIrisAttendRecord = new OdsIrisAttendRecord();
        if (req != null) {
            BeanUtils.copyProperties(req, odsIrisAttendRecord);
             current = req.getCurrent();
            size = req.getPageSize();
        }
        LambdaQueryWrapper<OdsIrisAlcoholQuestionRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .like(StrUtil.isNotBlank(req.getNameOrPersonNumber()), OdsIrisAlcoholQuestionRecord::getPersonName, req.getNameOrPersonNumber()).or()
                .like(StrUtil.isNotBlank(req.getNameOrPersonNumber()), OdsIrisAlcoholQuestionRecord::getPersonNumber, req.getNameOrPersonNumber())
                .eq(StrUtil.isNotBlank(req.getAlcoholResult()),OdsIrisAlcoholQuestionRecord::getAlcoholResult,req.getAlcoholResult())
                .eq(StrUtil.isNotBlank(req.getDepartName()),OdsIrisAlcoholQuestionRecord::getDepartName,req.getDepartName())
                .ge(req.getBeginDateTime() != null, OdsIrisAlcoholQuestionRecord::getRecognizeTime, req.getBeginDateTime())
                .le(req.getEndDateTime() != null, OdsIrisAlcoholQuestionRecord::getRecognizeTime, req.getEndDateTime())
                .orderByDesc(OdsIrisAlcoholQuestionRecord::getRecognizeTime);
        Page<OdsIrisAlcoholQuestionRecord> userPage = odsIrisAlcoholQuestionRecordService.page(new Page<>(current, size), queryWrapper);



        return R.ok(userPage);
    }






}
